﻿// =================================================================== 
// 模块说明
//====================================================================
// 创建时间：2018-12-15 16:33:03
// 负责人：liucx
// ===================================================================
using System;
using System.Data;
using System.IO;
using System.Configuration;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
using log4net.Layout.Pattern;
using OF.Component.Model;

namespace LogCenter.LogCustom
{
    #region 入库sql和参数
    public class InterfaceLogAdoAppender : InterfaceLogInfoCustomAdoNetAppender
    {
        public InterfaceLogAdoAppender()
        {
            //System.Data.SqlClient
            Type t = typeof(System.Data.SqlClient.SqlConnection);
            string s = t.Assembly.FullName.ToString();
            //数据连接类型
            this.ConnectionType = $"System.Data.SqlClient.SqlConnection, {s}";
            base.UseTransactions = false;
            this.CommandType = CommandType.Text;
            //sql语句
            this.CommandText = @"INSERT INTO InterfaceLog			
			(			
			[APIPath],
			[RequestParm],
			[ResponseResult],
			[RequestDate],
			[ResponseDate],
			[CallSource],
			[KeyField],
			[ResponseStatus]
			)
			 VALUES
			(			
			@APIPath,
			@RequestParm,
			@ResponseResult,
			@RequestDate,
			@ResponseDate,
			@CallSource,
			@KeyField,
			@ResponseStatus
			)
			 SET @InterfaceLogID=@@IDENTITY
			";
            string parameterName = "";
            parameterName = "InterfaceLogID";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int64, Layout = new Layout2RawLayoutAdapter(new InterfaceLogLayout("%" + parameterName)) });
            parameterName = "APIPath";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 200, Layout = new Layout2RawLayoutAdapter(new InterfaceLogLayout("%" + parameterName)) });
            parameterName = "RequestParm";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 2000, Layout = new Layout2RawLayoutAdapter(new InterfaceLogLayout("%" + parameterName)) });
            parameterName = "ResponseResult";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 2000, Layout = new Layout2RawLayoutAdapter(new InterfaceLogLayout("%" + parameterName)) });
            parameterName = "RequestDate";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.DateTime, Layout = new Layout2RawLayoutAdapter(new InterfaceLogLayout("%" + parameterName)) });
            parameterName = "ResponseDate";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.DateTime, Layout = new Layout2RawLayoutAdapter(new InterfaceLogLayout("%" + parameterName)) });
            parameterName = "CallSource";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int32, Layout = new Layout2RawLayoutAdapter(new InterfaceLogLayout("%" + parameterName)) });
            parameterName = "KeyField";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.String, Size = 50, Layout = new Layout2RawLayoutAdapter(new InterfaceLogLayout("%" + parameterName)) });
            parameterName = "ResponseStatus";
            this.AddParameter(new AdoNetAppenderParameter() { ParameterName = "@" + parameterName, DbType = DbType.Int32, Layout = new Layout2RawLayoutAdapter(new InterfaceLogLayout("%" + parameterName)) });

            base.ActivateOptions();
        }
    }
    #endregion

    #region 数据库连接
    public class InterfaceLogInfoCustomAdoNetAppender : AdoNetAppender
    {
        /// <summary>
        /// 数据库连接
        /// </summary>
        public new string ConnectionStringName
        {
            set
            {
                this.ConnectionString = ConfigurationManager.ConnectionStrings[value].ConnectionString;
            }
        }
    }
    #endregion

    #region 字段转换
    public class InterfaceLogLayout : PatternLayout
    {
        public InterfaceLogLayout(string pattern)
            : base(pattern)
        {

        }
        public override void ActivateOptions()
        {

            this.AddConverter("InterfaceLogID", typeof(InterfaceLogInterfaceLogIDConverter));
            this.AddConverter("APIPath", typeof(InterfaceLogAPIPathConverter));
            this.AddConverter("RequestParm", typeof(InterfaceLogRequestParmConverter));
            this.AddConverter("ResponseResult", typeof(InterfaceLogResponseResultConverter));
            this.AddConverter("RequestDate", typeof(InterfaceLogRequestDateConverter));
            this.AddConverter("ResponseDate", typeof(InterfaceLogResponseDateConverter));
            this.AddConverter("CallSource", typeof(InterfaceLogCallSourceConverter));
            this.AddConverter("KeyField", typeof(InterfaceLogKeyFieldConverter));
            this.AddConverter("ResponseStatus", typeof(InterfaceLogResponseStatusConverter));
            base.ActivateOptions();
        }
    }
    internal sealed class InterfaceLogInterfaceLogIDConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            InterfaceLogInfo log = (InterfaceLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.InterfaceLogID);
        }
    }
    internal sealed class InterfaceLogAPIPathConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            InterfaceLogInfo log = (InterfaceLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.APIPath);
        }
    }
    internal sealed class InterfaceLogRequestParmConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            InterfaceLogInfo log = (InterfaceLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.RequestParm);
        }
    }
    internal sealed class InterfaceLogResponseResultConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            InterfaceLogInfo log = (InterfaceLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ResponseResult);
        }
    }
    internal sealed class InterfaceLogRequestDateConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            InterfaceLogInfo log = (InterfaceLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.RequestDate);
        }
    }
    internal sealed class InterfaceLogResponseDateConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            InterfaceLogInfo log = (InterfaceLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ResponseDate);
        }
    }
    internal sealed class InterfaceLogCallSourceConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            InterfaceLogInfo log = (InterfaceLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.CallSource);
        }
    }
    internal sealed class InterfaceLogKeyFieldConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            InterfaceLogInfo log = (InterfaceLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.KeyField);
        }
    }
    internal sealed class InterfaceLogResponseStatusConverter : PatternLayoutConverter
    {
        override protected void Convert(TextWriter writer, LoggingEvent loggingEvent)
        {
            InterfaceLogInfo log = (InterfaceLogInfo)loggingEvent.MessageObject;
            if (log != null)
                writer.Write(log.ResponseStatus);
        }
    }
    #endregion

}